/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.agi.util.showdata;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

/**
 *
 * @author Renato
 */
public class ApresentadorGrafico implements ApresentadorDados {

    protected BufferedImage buf;

    public BufferedImage getBufferedImage() {
        return this.buf;
    }

    public ApresentadorGrafico() {
        this.width = 200;
        this.buscadorDeFinal = new BuscadorDeFinalPadrao();
        this.pintor = new PintorPadrao();
    }
    private BuscadorDeFinal buscadorDeFinal;
    private Pintor pintor;
    private int width;

    @Override
    public void apresentarDados(double[] data) {
        int meio;

        meio = buscadorDeFinal.getFinal(data);

        buf = new BufferedImage(meio, width, BufferedImage.TYPE_INT_RGB);

        double major, minor, fator;
        major = 0;
        minor = data[0];
        for (int i = 0; i < meio; i++) {
            if (data[i] > major) {
                major = data[i];
            }
            if (data[i] < minor) {
                minor = data[i];
            }
        }
        fator = width / (major - minor);
        
        double firstPoint;
        firstPoint = (data[0] - minor) * fator;
        
        pintor.iniciarPintura(buf,firstPoint);
        
        double point;
        for (int x = 0; x < meio; x++) {
            point = (data[x] - minor) * fator;
            pintor.printData(x, point);
        }
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public void setPintor(Pintor pintor){
        this.pintor=pintor;
    }
    
    public Pintor getPintor(){
        return pintor;
    }
    
    public void setBuscadorDeFinal(BuscadorDeFinal buscador){
        this.buscadorDeFinal=buscador;
    }
    
    public interface BuscadorDeFinal {

        public int getFinal(double[] data);
    }

    private class BuscadorDeFinalPadrao implements BuscadorDeFinal {

        @Override
        public int getFinal(double[] data) {
            return data.length;
        }
    }
    
}
